package tt.dz.dao.ext.common;

import org.springframework.stereotype.Repository;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsFactoryList;
import tt.dz.entity.exception.OmsDaoException;

import java.util.List;
/**
 * 厂商操作的数据库访问类
 * @author huanghao
 * @date 2016年1月5日
 */
@Repository
public class OmsFactoryListDao extends OmsCommonDao<OmsFactoryList>
{
	/**
	 * 分页查询相关的厂商数据信息
	 * 
	 * 这里没有关联查询出相应的运营商，名称。
	 * 因为厂商表中的运营商id，可能并不存在
	 * @param param 封装的分页查询的相关参数
	 * @return
	 * @throws OmsDaoException
	 */
	public List<OmsFactoryList> pagingFindFactoryList(OmsCommonParam param,OmsFactoryList obj)
			throws OmsDaoException
	{
		checkParam(param);
		addValidFlagParam2Query(param);
		try
		{
			return this.pagingFind(param, obj);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
	}
	/**
	 * 添加工厂信息
	 * @param factory
	 * @throws OmsDaoException
	 */
	public void addFactoryList(OmsFactoryList factory)throws OmsDaoException
	{
		checkParam(factory);
		int flag =  0;
		try
		{
			flag = this.save(factory);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("insert fail");
		}
	}
	/**
	 * 更新工厂信息
	 * @param factory
	 * @throws OmsDaoException
	 */
	public void updateFactoryList(OmsFactoryList factory)throws OmsDaoException
	{
		checkParam(factory);
		int flag =  0;
		try
		{
			flag = this.updateObject(factory);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("update fail");
		}
	}
	
	public void updateFactoryByOperId(String operId)throws OmsDaoException
	{
		int flag =  0;
		try
		{
			Object[] args = {operId};
			flag = this.getJdbcTemplate().update("update oms_factory_list set oper_merchant_id = null where oper_merchant_id = ?", args);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("update fail");
		}
	}
	/**
	 * 根据工厂id逻辑删除相应的工厂信息
	 * @param fid
	 * @throws OmsDaoException
	 */
	public void deleteFactoryList(String fid)throws OmsDaoException
	{
		checkParam(fid);
		int flag =  0;
		try
		{
			flag = this.logicDeleteObjectBySinglePk(OmsFactoryList.class, fid);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e.getMessage(),e);
		}
		if (flag < 0)
		{
			throw new OmsDaoException("del fail");
		}
		
	}
	
	public int findCountFactoryList(OmsCommonParam param)throws OmsDaoException
	{
		checkParam(param);
		addValidFlagParam2Query(param);
		try
		{
			return this.queryTotalByObject(OmsFactoryList.class, param.getParamsMap());
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
		
	}
	/**
	 * 获取最大
	 * @return
	 * @throws OmsDaoException
	 */
	public  String findMaxIdFactryList()throws OmsDaoException
	{
		String sql = "select max(factory_id) as maxId from oms_factory_list";
		try
		{
			return (String) this.getJdbcTemplate().queryForObject(sql, String.class);
		}
		catch(Exception e)
		{
			logError(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
}
